/**
 * 存货模块
 * @author jinhua.gu
 * @date 2015-11-3
 */
angular.module('baisonCunhuo', ['baisonDb', 'baisonService'])
//要货
        .factory('yaoHuo', ['apiService', '$rootScope', 'timeStamp2String', 'changeDate', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert','ThirdDev',
            function (apiService, $rootScope, timeStamp2String, changeDate, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert,ThirdDev) {
                //要货申请-配货查询(按照时间查询)
                var configData = $api.getStorage('configData');
                var shop_code = $rootScope.current_shop.ShopCode;
                var peihuo_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.shop_code = shop_code;
                    param.record_code = filter.record_code;
                    return apiService('stock/dim_allocate_order_record/search_group_data_by_cond', param);
                };
                //默认拿服务器数据()
                var peihuo_search_api = function (param) {
                    return apiService('stock/dim_allocate_order_record/search_group_data', param);
                };
                //要货小票单据明细（在线查询）
                var peihuo_list_detail_search_by_code = function (filter) {
                    return apiService('stock/dim_allocate_order_record/get_detail', filter);
                };
                //生成单据编号
                var create_fast_bill_sn = function (type) {
                    var length = 2;
                    var prefix = '';
                    switch (type) {
                        case 'allocate_order':
                            prefix = 'PH';
                            break;
                        case 'return':
                            prefix = 'SD';
                            break;
                        case 'receipt':
                            prefix = 'RP';
                            break;
                        case 'store_shift':
                            prefix = 'SP';
                            break;
                        case 'pandian':
                            prefix = 'KC';
                            break;
                    }

                    return prefix + timeStamp2String(0, 'datetime2') + generate_rand(length);
                };
                var generate_rand = function (length) {
                    var rand = '';
                    for (var i = 0; i < length; ++i) {
                        rand += Math.floor(Math.random() * 10).toString();
                    }
                    return rand;
                };
                //单据配货类型下拉
                var record_type_list = function (filter) {
                    var where = ' 1=1 ';
                    var sql;
                    $.each(filter, function (index, n) {
                        where = where + ' and ' + index + '="' + n + '"';
                    });
                    sql = 'select * from pos_base_record_type where ' + where;
                    return dbSelect('YsPos', sql);
                };
                //仓库下拉
                var select_storage_list = function (filter) {
                    //林清轩
                    if(ThirdDev.is_lqx()){  //$rootScope.purview['\/cunhuo\/chuku'].show == 0
                    	var where = ' 1=1 ';
	                    var sql;
	                    $.each(filter, function (index, n) {
	                        where = where + ' and ' + index + '="' + n + '"';
	                    });
	                    sql = 'select * from Pos_base_store where ' + where;
                    }else{
                       //lite
                        var shop_code = $rootScope.current_shop.ShopCode;
                        var status = 1;
                 		var sql = 'select p.* from Pos_base_store p inner join PosShop s on s.Id=p.pid where p.status = "'+status+'" and s.ShopCode="'+shop_code+'"';
                    }                  
                    return dbSelect('YsPos', sql);
                };
                //新增配货单  必填信息  保存到本地
                var save_local = function (data) {
                    var day = timeStamp2String(0, 'date');
                    var shop_code = $rootScope.current_shop.ShopCode;
                    var shop_name = $rootScope.current_shop.ShopName;
                    var shop_store_code = $rootScope.current_shop.ShopCode;
//                  var store_code = data.store_code;
                    //lqx
                    if (ThirdDev.is_lqx()) {  //$rootScope.purview['\/cunhuo\/chuku'].show == 0
                        var store_code = '000';
                        var store_name = '总仓';
                    } else if (!ThirdDev.is_lqx()) {  //$rootScope.purview['\/cunhuo\/chuku'].show == 1
                        var store_code = data.store_code;
                        var store_name = '';
                    }
                    var record_code = data.record_code;
                    var record_time = data.record_time;
                    var allocate_type = data.record_type_code;
                    //营业员
                    var user_code = data.user_code.UserCode;
                    var is_add_person = $rootScope.user.UserCode;  //当前登录用户
                    var is_add_time = timeStamp2String(0, 'datetime');
                    var record = {
                        'record_code': record_code,
                        'record_time': record_time,
                        'shop_code': shop_code,
                        'shop_name': shop_name,
                        'shop_store_code': shop_store_code,
                        'store_code': store_code,
                        'store_name':store_name,
                        'allocate_type': allocate_type,
                        'user_code': user_code,
                        'is_add_person': is_add_person,
                        'is_add_time': is_add_time,
                        'lastchanged': day
                    };
                    return record;
                };
                //根据仓库代码获取仓库名称
                var getStoreName_by_code = function (code) {
                    var sql = 'select store_name from Pos_base_store where store_code="' + code + '"';
                    return dbSelect('YsPos', sql);
                };
                //保存配货单据信息（暂时没用）
                var save_peihuo_local = function (__record, __detail) {
                    dbTrans.begin().then(function (success) {
                        return dbUpdata('store_record', {
                            'num': __record.num,
                            'money': __record.money
                        }, {
                            'record_code': __record.record_code
                        }).then(function (success) {
                            return dbDelete('store_record_detail', {
                                'record_code': __record.record_code
                            }).then(function (success) {
                                $.each(__detail, function (i, n) {
                                    return dbInsert('store_record_detail', n, function (ret) {
                                    });
                                });
                            });
                        });
                    }).then(function (success) {
                        dbTrans.commit();
                    }).catch(function (err) {
                        dbTrans.rollback();
                    });
                };
                //上传配货单据到服务器
                var upload_peihuo_remote = function (filter) {
                    return apiService('stock/dim_allocate_order_record/add', filter);
                };
                ////////////////////////////////////////////////
                //根据要货类型搜索商品sku
                var sku_search_by_sku = function (keywords, cate_code) {
                    var sql;
                    //暂时不用考虑cate_code不等于‘’的情况
                    if (cate_code != '') {
                        sql = 'select s.* from PosSku s inner join PosGoods g where s.SkuCode like "%' + keywords + '" and s.GoodsCode=g.GoodsCode and g.CategoryCode="' + cate_code + '"';
                   		return dbSelect('YsPos', sql);
                    } else {
                    	var configData = $api.getStorage('configData');
                        var conditionStr = "";
                        switch(configData.searchMacthType)
                        {
                            case "match_tail": conditionStr = "%" + keywords;
                            break;
                            case "match_head": conditionStr = keywords + "%";
                            break;
                            case "match_any": conditionStr = "%" + keywords + "%";
                            break;
                            default:
                            conditionStr = "%" + keywords;
                            break;
                        }
                        var sql_barcode = "select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where s.SkuCode = (select SkuCode from PosBarcode where BarCode = '" + keywords + "') and g.GoodsStatus = 1  ";

                        //注意 异步查询
                        return dbSelect('YsPos', sql_barcode).then(function(ret){
                            if(ret == undefined || ret.length < 1){
                                var sql = 'select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where (s.SkuCode like "' 
                        + conditionStr + '" or s.GoodsCode like "'+ conditionStr +'") and g.GoodsStatus = 1;';
                                return dbSelect('YsPos', sql);
                            }else{
                                return ret;
                            }
                        });
//                      sql = 'select * from PosSku where SkuCode like "%' + keywords + '"';
// 						return dbSelect('YsPos', sql);
                    }
                   
                };
                //根据配货类型自动加载建议要货数
                var getAdviseNum_by_allocate_type = function (cate_code) {
                    var sql;
                    //暂时不用考虑cate_code不等于‘’的情况
                    if (cate_code != '') {
                        sql = 'select s.* from PosSku s inner join PosGoods g where s.GoodsCode=g.GoodsCode and g.CategoryCode="' + cate_code + '"';
                    } else {
                        sql = 'select * from PosSku';
                    }
                    return dbSelect('YsPos', sql);
                };
                //获取安全库存天数、系数等配置项
                var get_item = function () {
                    return apiService('pos/advise/get_info', {});
                };

                //获取近30天日均销量
                var get_sales_by_day = function (sku, day) {
                    var date = changeDate(day, 1);
                    var sql = 'select sum(d.num) total_num from PosReceiptDetail d left join PosReceiptRecord r on r.record_code = d.record_code where  r.record_time >= "' + date + '" and d.sku="' + sku.SkuCode + '" and r.is_return=0 and r.is_checkout=1';
                    return dbSelect('YsPos', sql);
                };
                return {
                    peihuo_select: peihuo_select,
                    peihuo_search_api: peihuo_search_api,
                    peihuo_list_detail_search_by_code: peihuo_list_detail_search_by_code,
                    create_fast_bill_sn: create_fast_bill_sn, //生成单据编号
                    generate_rand: generate_rand,
                    record_type_list: record_type_list, //单据配货类型下拉
                    select_storage_list: select_storage_list, //仓库下拉
                    sku_search_by_sku: sku_search_by_sku, //商品sku查询
                    save_local: save_local, //新增配货单必填信息保存到本地
                    save_peihuo_local: save_peihuo_local, //保存单据配货信息
                    upload_peihuo_remote: upload_peihuo_remote, //上传单据信息
                    get_item: get_item,
                    get_sales_by_day: get_sales_by_day,
                    getAdviseNum_by_allocate_type: getAdviseNum_by_allocate_type,
                    getStoreName_by_code: getStoreName_by_code

                };
            }])
        .factory('ruKu', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert',
            function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert) {
                //var shop_code = $rootScope.current_shop.ShopCode;
                //服务器查找
                var ruku_search_api = function (param) {
                    return apiService('stock/dim_allocate_record/search_group_data', param);
                };
                //根据入库时间查找
                var ruku_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    } else {
                        param.record_time_start = filter.record_time_start;
                        param.record_time_end = filter.record_time_end;
                    }
                    param.shop_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    param.is_shop_stock_in = filter.fOrder;
                    return apiService('stock/dim_allocate_record/search_group_data_by_cond', param);
                };

                //入库单单据明细
                var ruku_list_detail_search_by_code = function (param) {
                    return apiService('stock/dim_allocate_record/get_detail', param);
                };
                //入库
                var allocate_record_stock_in_detail = function (param) {
                    return apiService('stock/dim_allocate_record/stock_in_detail', param);
                };
                //更新本地库存
                var update_local_SkuNum = function (sku, num) {
                    return dbSelect('YsPos', 'select SkuNum from PosSku where SkuCode="' + sku + '"').then(function (ret) {
                        var SkuNum = parseFloat(ret[0].SkuNum) + parseFloat(num);
                        return dbUpdate('PosSku', {'SkuNum': SkuNum}, {'SkuCode': sku});
                    });
                };
//      //更新库存到服务器
//      var update_service_SkuNum = function(sku,num){
//      	var param = {};
//      	param.store_code = $rootScope.current_shop.ShopCode ;
//      	param.sku_code = sku;
//      	param.sku_num = num;
//      	apiService('base/goods/update_goods_inv_num_by_skus', param);
//      }
                return {
                    ruku_search_api: ruku_search_api, //入库在线查找
                    ruku_select: ruku_select,
                    ruku_list_detail_search_by_code: ruku_list_detail_search_by_code, //入库单单据明细
                    allocate_record_stock_in_detail: allocate_record_stock_in_detail, //入库操作
                    update_local_SkuNum: update_local_SkuNum
                };
            }])
        .factory('tuiKu', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert', 'dbReplace','ThirdDev',
            function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert, dbReplace,ThirdDev) {
                //var shop_code = $rootScope.current_shop.ShopCode;
                var configData = $api.getStorage('configData');
                //服务器查找（在线）
                var tuiku_search_api = function (param) {
                    return apiService('stock/dim_return_record/search_group_data', param);
                };
                //退库单单据明细
                var tuiku_list_detail_search_by_code = function (param) {
                    return apiService('stock/dim_return_record/get_detail', param);
                };
                //根据时间查询
                var tuiku_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.shop_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/dim_return_record/search_group_data_by_cond', param);
                };
                //保存必填信息到本地
                var save_local = function (param) {
                    var day = timeStamp2String(0, 'date');
                    var shop_code = $rootScope.current_shop.ShopCode;
                    var shop_name = $rootScope.current_shop.ShopName;
                    var shop_store_code = $rootScope.current_shop.ShopCode;
                    //lqx
                    if (ThirdDev.is_lqx()) {  //$rootScope.purview['\/cunhuo\/chuku'].show == 0
                        var store_code = '000';
                        var store_name = '总仓';
                    } else if (!ThirdDev.is_lqx()) {  //$rootScope.purview['\/cunhuo\/chuku'].show == 1
                        var store_code = param.store_code;
                        var store_name = '';
                    }
                    var record_code = param.record_code;
                    var record_time = param.record_time;
                    var return_type = param.return_type.record_type_code;
                    //退货类型
                    var user_code = param.user_code.UserCode;
                    //营业员类型
                    var is_add_person = $rootScope.user.UserCode;
                    var is_add_time = timeStamp2String(0, 'datetime');
//                  var th_name = param.tuihuo_code.name;  //退货原因
                    var record = {
                        'record_code': record_code,
                        'record_time': record_time,
                        'shop_code': shop_code,
                        'shop_name': shop_name,
                        'shop_store_code': shop_store_code,
                        'store_code': store_code,
                        'store_name': store_name,
                        'return_type': return_type,
                        'user_code': user_code,
                        'is_add_person': is_add_person,
                        'is_add_time': is_add_time,
                        'lastchanged': day,
                        'remark': ''
                    };
                    return record;
                };
                //搜索sku(新增退库单)
                var sku_search_by_sku = function (keywords) {
                    	var configData = $api.getStorage('configData');
                        var conditionStr = "";
                        switch(configData.searchMacthType)
                        {
                            case "match_tail": conditionStr = "%" + keywords;
                            break;
                            case "match_head": conditionStr = keywords + "%";
                            break;
                            case "match_any": conditionStr = "%" + keywords + "%";
                            break;
                            default:
                            conditionStr = "%" + keywords;
                            break;
                        }
                        var sql_barcode = "select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where s.SkuCode = (select SkuCode from PosBarcode where BarCode = '" + keywords + "') and g.GoodsStatus = 1  ";

                        //注意 异步查询
                        return dbSelect('YsPos', sql_barcode).then(function(ret){
                            if(ret == undefined || ret.length < 1){
                                var sql = 'select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where (s.SkuCode like "' 
                        + conditionStr + '" or s.GoodsCode like "'+ conditionStr +'") and g.GoodsStatus = 1;';
                                return dbSelect('YsPos', sql);
                            }else{
                                return ret;
                            }
                        });
//                  var sql = 'select * from PosSku where SkuCode like "%' + keywords + '"';
//                  return dbSelect('YsPos', sql);
                };

                //添加退库单据信息到服务器
                var return_record_add_api = function (param) {
                    return apiService('stock/dim_return_record/add_by_pos', param);
                };
                //退库操作（换成终端出仓单使用，林清轩暂时使用这个）
                var return_record_stock_in_detail = function (param) {
                    return apiService('stock/dim_return_record/stock_out', param);
                };
                //更新库存（换成终端出仓单使用，林清轩暂时使用这个）
                var update_local_SkuNum = function (sku, num) {
                    return dbSelect('YsPos', 'select SkuNum from PosSku where SkuCode="' + sku + '"').then(function (ret) {
                        var SkuNum = parseFloat(ret[0].SkuNum) - parseFloat(num);
                        return dbUpdate('PosSku', {'SkuNum': SkuNum}, {'SkuCode': sku});
                    });
                };
                return {
                    tuiku_search_api: tuiku_search_api, //服务器查找（在线）
                    tuiku_list_detail_search_by_code: tuiku_list_detail_search_by_code, //退库单单据明细
                    tuiku_select: tuiku_select,
                    save_local: save_local,
                    sku_search_by_sku: sku_search_by_sku,
                    return_record_add_api: return_record_add_api,
                    return_record_stock_in_detail: return_record_stock_in_detail,
                    update_local_SkuNum: update_local_SkuNum
                };
            }])

        //出库
        .factory('chuku', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert', 'dbReplace', function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert, dbReplace) {
                //服务器查找    终端出仓单据
                var chuku_search_api = function (param) {
                    return apiService('stock/dim_return_record/search_stock_out_data', param);
                };
                //出仓单单据明细(单号)
                var chuku_list_detail_search_by_code = function (param) {
                    return apiService('stock/dim_return_record/search_stock_out_data_detail', param);
                };
                //根据出库条件查询
                var chuku_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    } else {
                        param.record_time_start = filter.record_time_start;
                        param.record_time_end = filter.record_time_end;
                    }
                    param.shop_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/dim_return_record/search_stock_out_data_by_cond', param);
                };
                //出库操作
                var allocate_record_stock_out_detail = function (param) {
                    return apiService('stock/dim_return_record/stock_out', param);
                };
                //更新本地库存
                var update_local_SkuNum = function (sku, num) {
                    return dbSelect('YsPos', 'select SkuNum from PosSku where SkuCode="' + sku + '"').then(function (ret) {
                        var SkuNum = parseFloat(ret[0].SkuNum) - parseFloat(num);
                        return dbUpdate('PosSku', {'SkuNum': SkuNum}, {'SkuCode': sku});
                    });
                };
                return {
                    chuku_search_api: chuku_search_api,
                    chuku_list_detail_search_by_code: chuku_list_detail_search_by_code,
                    allocate_record_stock_out_detail: allocate_record_stock_out_detail,
                    update_local_SkuNum: update_local_SkuNum,
                    chuku_select: chuku_select
                };
            }])
        .factory('diaoBo', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert','ThirdDev',
            function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert,ThirdDev) {
                var shop_code = $rootScope.current_shop.ShopCode;
                var configData = $api.getStorage('configData');
                //服务器在线查找
                var diaobo_search_api = function (param) {
                    return apiService('stock/stm_store_shift_record/search_group_data', param);
                };
                //调拨单查询（时间）
                var diaobo_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.shop_code = shop_code;
                    param.record_code = filter.record_code;
                    return apiService('stock/stm_store_shift_record/search_group_data_by_cond', param);
                };
                //调拨单据明细
                var diaobo_list_detail_search_by_code = function (param) {
                    return apiService('stock/stm_store_shift_record/get_detail', param);
                };
                //获取邻店
                var shop_list_get = function (keywords) {
                    var shop_code = $rootScope.current_shop.ShopCode;
                    if (keywords == '') {
                        return dbSelect('YsPos', 'select * from PosShop where ShopCode != "' + shop_code + '"');
                    } else {
                        return dbSelect('YsPos', 'select ShopName from PosShop where ShopCode = "' + shop_code + '"').then(function (response) {
                            return dbSelect(
                                    'YsPos',
                                    'select * from PosShop where  ShopCode != "' + shop_code + '" and  ShopName != "' + response[0].ShopName + '" and  (ShopCode = "' + keywords + '" or ShopName like "%' + keywords + '%")');
                        });
                    }


                };
                //保存本地必填信息
                var save_local = function (param) {
                    var day = timeStamp2String(0, 'date');
                    var shop_code = $rootScope.current_shop.ShopCode;
                    var shift_in_store_code = $rootScope.current_shop.ShopCode;
                    var shift_in_store_name = $rootScope.current_shop.ShopName;//增加名字(移入方)
                    //lqx
                    if (ThirdDev.is_lqx()) {  //$rootScope.purview['\/cunhuo\/chuku'].show == 0
                        var shift_out_store_code = param.friend_shop_code.friend_shop_code;
                        var shift_out_store_name = param.friend_shop_code.friend_shop_name;//增加名字(发货方)
                    } else {
                        var shift_out_store_code = param.friend_shop_code.friend_shop_code;
                        var shift_out_store_name = param.friend_shop_code.friend_shop_name;//增加名字(发货方)
                    }
                    var record_code = param.record_code;
                    var record_time = param.record_time;
                    var shift_type = param.shift_type.record_type_code;
                    var user_code = param.user_code.UserCode;
                    var is_add_person = $rootScope.user.UserCode;
                    var is_add_time = timeStamp2String(0, 'datetime');
                    var record = {
                        'record_code': record_code,
                        'record_time': record_time,
                        'shop_code': shop_code,
                        'shift_in_store_code': shift_in_store_code,
                        'shift_in_store_name': shift_in_store_name,
                        'shift_out_store_code': shift_out_store_code,
                        'shift_out_store_name': shift_out_store_name,
                        'shift_type': shift_type,
                        'shift_property': 1,
                        'user_code': user_code,
                        'is_add_person': is_add_person,
                        'is_add_time': is_add_time,
                        'lastchanged': day
                    };

                    return record;
                };
                //搜索sku(新增调拨单)
                var sku_search_by_sku = function (keywords) {
                		var configData = $api.getStorage('configData');
                        var conditionStr = "";
                        switch(configData.searchMacthType)
                        {
                            case "match_tail": conditionStr = "%" + keywords;
                            break;
                            case "match_head": conditionStr = keywords + "%";
                            break;
                            case "match_any": conditionStr = "%" + keywords + "%";
                            break;
                            default:
                            conditionStr = "%" + keywords;
                            break;
                        }
                        var sql_barcode = "select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where s.SkuCode = (select SkuCode from PosBarcode where BarCode = '" + keywords + "') and g.GoodsStatus = 1  ";

                        //注意 异步查询
                        return dbSelect('YsPos', sql_barcode).then(function(ret){
                            if(ret == undefined || ret.length < 1){
                                var sql = 'select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where (s.SkuCode like "' 
                        + conditionStr + '" or s.GoodsCode like "'+ conditionStr +'") and g.GoodsStatus = 1;';
                                return dbSelect('YsPos', sql);
                            }else{
                                return ret;
                            }
                        });
//                  var sql = 'select * from PosSku where SkuCode like "%' + keywords + '"';
//                  return dbSelect('YsPos', sql);
                };
                //上传到服务器
                var return_record_add_api = function (param) {
                    return apiService('stock/stm_store_shift_record/add', param);
                };
                //调拨申请单（向我要货）执行生成终端退仓单
                var diaobo_record_execute = function(record_code){
                    var param = {};
                    param.record_code = record_code;
                    param.is_add_person = $rootScope.user.UserCode;
                	return apiService('stock/stm_store_shift_record/execute',param);
                };
                return {
                    diaobo_search_api: diaobo_search_api,
                    diaobo_select: diaobo_select,
                    diaobo_list_detail_search_by_code: diaobo_list_detail_search_by_code,
                    shop_list_get: shop_list_get,
                    save_local: save_local, //必填信息的保存
                    return_record_add_api: return_record_add_api,
                    sku_search_by_sku: sku_search_by_sku,
                    diaobo_record_execute:diaobo_record_execute
                };
            }])
        .factory('diaoChu', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert',
            function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert) {
                //从服务器获取
                var diaochu_search_api = function (param) {
                    return apiService('stock/stm_store_shift_record/search_group_data', param);
                };
                //根据时间进行查找
                var diaochu_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.shift_out_store_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/stm_store_shift_record/search_group_data_by_cond', param);
                };
                //调出单据明细
                var diaochu_list_detail_search_by_code = function (param) {
                    return apiService('stock/stm_store_shift_record/get_detail', param);
                };
                //出库
                var store_shift_record_store_out = function (param) {
                    return apiService('stock/stm_store_shift_record/store_out', param);
                };
                //更新库存
                var update_local_SkuNum = function (sku, num) {
                    return dbSelect('YsPos', 'select SkuNum from PosSku where SkuCode="' + sku + '"').then(function (ret) {
                        var SkuNum = parseFloat(ret[0].SkuNum) - parseFloat(num);
                        return dbUpdate('PosSku', {'SkuNum': SkuNum}, {'SkuCode': sku});
                    });
                };
                //++++++++++++++++++++lite地址使用这个方法+++++++++++++++++++++++++++++
                //调出单
                var shift_out_search_group_data =function(param){
                	return apiService('stock/stm_store_shift_record/shift_out_search_group_data',param);
                };
                //调出单根据时间查找
                var shift_out_select = function(filter){
                	var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.shop_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/stm_store_shift_record/shift_out_search_group_data_by_cond', param);
                };
                //横调出单据明细
                var shift_out_detail = function(param){
                	return apiService('stock/stm_store_shift_record/shift_out_get_detail',param);
                };
                return {
                    diaochu_search_api: diaochu_search_api,
                    diaochu_select: diaochu_select,
                    diaochu_list_detail_search_by_code: diaochu_list_detail_search_by_code,
                    store_shift_record_store_out: store_shift_record_store_out,
                    update_local_SkuNum: update_local_SkuNum,
                    shift_out_search_group_data:shift_out_search_group_data,
                    shift_out_select:shift_out_select,
                    shift_out_detail:shift_out_detail
                };
            }])
            .factory('diaoRu', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert',
    function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert) {
        //从服务器获取
        var diaoru_search_api = function (param) {
            return apiService('stock/stm_store_shift_record/search_group_data', param);
        };
        //根据时间进行查询
        var diaoru_select = function (filter) {
            var param = {};
            if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
            }
            param.shift_in_store_code = $rootScope.current_shop.ShopCode;
            param.record_code = filter.record_code;
            return apiService('stock/stm_store_shift_record/search_group_data_by_cond', param);
        };
        //调入单据明细
        var diaoru_list_detail_search_by_code = function (param) {
            return apiService('stock/stm_store_shift_record/get_detail', param);
        };
        //入库操作
        var store_shift_record_store_in = function (param) {
            return apiService('stock/stm_store_shift_record/store_in', param);
        };
        //更新库存
        var update_local_SkuNum = function (sku, num) {
            return dbSelect('YsPos', 'select SkuNum from PosSku where SkuCode="' + sku + '"').then(function (ret) {
                var SkuNum = parseFloat(ret[0].SkuNum) + parseFloat(num);
                return dbUpdate('PosSku', {'SkuNum': SkuNum}, {'SkuCode': sku});
            });
        };
        //++++++++++++++++++++lite地址使用这个方法+++++++++++++++++++++++++++++
        var shift_in_search_group_data = function(param){
        	return apiService('stock/stm_store_shift_record/shift_in_search_group_data',param);
        };
        //调入单根据时间查找
        var shift_in_select = function(filter){
              var param = {};
              if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                      param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                      param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                 }
                param.shop_code = $rootScope.current_shop.ShopCode;
                param.record_code = filter.record_code;
                return apiService('stock/stm_store_shift_record/shift_in_search_group_data_by_cond', param);
             };
         //横调入单据明细
         var shift_in_detail = function(param){
         	return apiService('stock/stm_store_shift_record/shift_in_get_detail',param);
         };
        return {
            diaoru_search_api: diaoru_search_api,
            diaoru_select: diaoru_select,
            diaoru_list_detail_search_by_code: diaoru_list_detail_search_by_code,
            store_shift_record_store_in: store_shift_record_store_in,
            update_local_SkuNum: update_local_SkuNum,
            shift_in_search_group_data:shift_in_search_group_data,
            shift_in_select:shift_in_select,
            shift_in_detail:shift_in_detail
        };
    }])
        .factory('panDian', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert', 'dbInsertMulti','ThirdDev',
            function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert, dbInsertMulti,ThirdDev) {
                var configData = $api.getStorage('configData');
                //从服务器获取数据
                var pandian_search_api = function (param) {
                    return apiService('stock/stm_take_stock_record/search_group_data', param);
                };
                //查询
                var pandian_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.store_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/stm_take_stock_record/search_group_data_by_cond', param);
                };
                //盘点单据明细（根据record_code）在线查询
                var pandian_list_detail_search_by_code = function (param) {
                    return apiService('stock/stm_take_stock_record/get_detail', param);
                };
                //获取全部离线盘点单 本地
                var get_list_local_not_upload = function (callback) {
                    var sql = 'select * from stm_take_stock_record where is_upload=0 and num>0 order by record_time desc';
                    return dbSelect('YsPos', sql);
                };
                //获取离线盘点单详细信息  本地
                var take_stock_get_wait_upload_detail = function (record_code) {
                    var sql = 'select d.*,r.take_stock_type from stm_take_stock_record r inner join stm_take_stock_record_detail d on r.record_code=d.record_code and d.record_code="' + record_code + '" and r.is_upload=0 and r.num>0 order by r.record_time desc';
                    return dbSelect('YsPos', sql);
                };
                //根据record_code获取离线盘点主单据   本地
                var take_stock_record_by_record_code = function (record_code) {
                    var sql = 'select * from stm_take_stock_record where record_code="' + record_code + '"';
                    return dbSelect('YsPos', sql);
                };

                //商品sku查询(明细级)(lite和林清轩公用：林清轩使用一个参数，lite使用两个参数)
                var sku_search_by_sku = function (keywords,brand_code) {
                	if(brand_code != '' && brand_code != undefined){
                		var configData = $api.getStorage('configData');
                        var conditionStr = "";
                        switch(configData.searchMacthType)
                        {
                            case "match_tail": conditionStr = "%" + keywords;
                            break;
                            case "match_head": conditionStr = keywords + "%";
                            break;
                            case "match_any": conditionStr = "%" + keywords + "%";
                            break;
                            default:
                            conditionStr = "%" + keywords;
                            break;
                        }
                        var sql_barcode = "select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where s.SkuCode = (select SkuCode from PosBarcode where BarCode = '" + keywords + "') and g.brand_code = '" + brand_code + "' and g.GoodsStatus = 1 ";

                        //注意 异步查询
                        return dbSelect('YsPos', sql_barcode).then(function(ret){
                            if(ret == undefined || ret.length < 1){
                                var sql = 'select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where (s.SkuCode like "' 
                        + conditionStr + '" or s.GoodsCode like "'+ conditionStr +'") and g.brand_code = "'+brand_code+'" and g.GoodsStatus = 1;';
                                return dbSelect('YsPos', sql);
                            }else{
                                return ret;
                            }
                        });
//              		var sql = 'select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where  g.brand_code = "'+brand_code+'" and s.SkuCode like "%' + keywords + '"';
//              		return dbSelect('YsPos', sql);
                	}else{
                		var configData = $api.getStorage('configData');
                        var conditionStr = "";
                        switch(configData.searchMacthType)
                        {
                            case "match_tail": conditionStr = "%" + keywords;
                            break;
                            case "match_head": conditionStr = keywords + "%";
                            break;
                            case "match_any": conditionStr = "%" + keywords + "%";
                            break;
                            default:
                            conditionStr = "%" + keywords;
                            break;
                        }
                        var sql_barcode = "select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where s.SkuCode = (select SkuCode from PosBarcode where BarCode = '" + keywords + "') and g.GoodsStatus = 1  ";

                        //注意 异步查询
                        return dbSelect('YsPos', sql_barcode).then(function(ret){
                            if(ret == undefined || ret.length < 1){
                                var sql = 'select s.* from PosSku s inner join PosGoods g on s.GoodsCode = g.GoodsCode where (s.SkuCode like "' 
                        + conditionStr + '" or s.GoodsCode like "'+ conditionStr +'") and g.GoodsStatus = 1;';
                                return dbSelect('YsPos', sql);
                            }else{
                                return ret;
                            }
                        });
//              		var sql = 'select * from PosSku where  SkuCode like "%' + keywords + '"';
//              		return dbSelect('YsPos', sql);
                	}               
                    
                };

                //获取所有盘点数据
                var sku_search = function (keywords) {
                    var sql = 'select GoodsCode,GoodsName,SkuCode,SkuNum from PosSku where SkuNum > 0 or SkuNum < 0';
                    return dbSelect('YsPos', sql);
                };

                //保存盘点单在本地=============================   
                var save_record_detail = function (_record, record_detail) {
                    var detail = [];
                    var stock_record = {};         //主表
                    $.each(record_detail, function (i, n) {
                        detail.push({
                            'record_code': n.record_code,
                            'sku': n.sku,
                            'goods_code': n.goods_code,
                            'goods_name': n.goods_name,
                            'goods_name_zh_tw': n.goods_name_zh_tw,
                            'goods_name_en': n.GoodsName_en,
                            'SkuNum': n.SkuNum,//账面数sku.SkuNum
                            'rebate':n.rebate,
                            'color_code': n.color_code,
                            'size_code': n.size_code,
                            'num': n.num,
                            'price': n.price,
                            'money': n.money
                        });
                    });
                    var record = [];
                    //林清轩
                    if(ThirdDev.is_lqx()){//$rootScope.purview['\/cunhuo\/pandianTask_list'].show == 0
                    	record.push(_record);
                    	stock_record = _record;
                    }else if(!ThirdDev.is_lqx()){ //$rootScope.purview['\/cunhuo\/pandianTask_list'].show == 1
                    	record.push({
                    		'record_code':_record.record_code,
                    		'relation_code':_record.relation_code,
                    		'record_time':_record.record_time,
                    		'user_code':_record.user_code,
//                  		'status':_record.status,
//                  		'brand_name':_record.brand_name,
//                  		'brand_code':_record.brand_code,
                    		'num':_record.num,
                    		'lastchanged':_record.time,
                    		'store_code':_record.store_code,
                    		'is_upload':_record.is_upload,
                    		'money':_record.money
//                  		'stock_type':_record.stock_type,
                    	});
                    	//将主表信息重新组合
                    	stock_record.record_code = _record.record_code;
                    	stock_record.relation_code = _record.relation_code;
                    	stock_record.record_time = _record.record_time;
                    	stock_record.user_code = _record.user_code;
                    	stock_record.num = _record.num;
                    	stock_record.lastchanged = _record.time;
                    	stock_record.store_code = _record.store_code;
                    	stock_record.is_upload = _record.is_upload;  
                    	stock_record.money = _record.money;                    	
                    }
                    dbTrans.begin().then(function (success) {
                        var sql = 'select * from stm_take_stock_record where record_code="' + stock_record.record_code + '"';
                        return dbSelect('YsPos', sql).then(function (ret) {
                            if (ret.length == 0) {
                                //盘点单据不存在就插入
                                return dbInsertMulti('stm_take_stock_record', record).then(function (ret) { 
                                    //插入盘点单据新明细
                                    if (detail) {
                                        return dbInsertMulti('stm_take_stock_record_detail', detail).then(function (ret) {
                                            baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], msg: $rootScope.language.MSG_SAVE_SUCCESS});
                                        });
                                    }
                                })
                            } else {
                                //盘点单据存在     更新盘点单据
                                return dbUpdate('stm_take_stock_record', stock_record, {
                                    'record_code': stock_record.record_code
                                }).then(function (ret) {
                                    //删除单据信息明细   
                                    return dbDelete('stm_take_stock_record_detail', {
                                        'record_code': stock_record.record_code
                                    }).then(function (ret) {
                                        //插入盘点单据新明细
                                        if (detail) {
                                            return dbInsertMulti('stm_take_stock_record_detail', detail).then(function (ret) {
                                                baison_app.api.toast({buttons: [$rootScope.language.COMMON_CONFIRM_BUTTON], msg: $rootScope.language.MSG_SAVE_SUCCESS});
                                            });
                                        }
                                    });
                                });
                            }
                        });
                    }).then(function (success) {
                        dbTrans.commit();
                    }).catch(function (err) {
                        dbTrans.rollback();
                    });
                };

                //获取保存的盘点据 
                var get_save_stock_record = function(){
                    var sql = 'select * from stm_take_stock_record where is_upload = 0';
                    return dbSelect('YsPos',sql);
                };
                //删除保存的盘点单
                var del_save_stock_record = function(){
                    var sql = 'delete from stm_take_stock_record where is_upload = 0';
                    return dbSelect('YsPos',sql);
                };
                //获取保存的盘点据商品详情信息
                var get_save_stock_record_detail = function(record_code){
                    var sql = 'select * from stm_take_stock_record_detail where record_code = "'+record_code+'"';
                    //console.log(sql);
                    return dbSelect('YsPos',sql);
                };

                //上传盘点单
                var take_stock_record_add_detail_api = function (_record, record_detail) {
                    var detail = [];
                    $.each(record_detail, function (i, n) {
                        detail.push({
                            'record_code': n.record_code,
                            'goods_code': n.goods_code,
                            'color_code': n.color_code,
                            'size_code': n.size_code,
                            'sku': n.sku,
                            'price': n.price,
                            'num': n.num
                        });
                    });

                    var record = {
                        'record_code': _record.record_code,
                        'store_code': _record.store_code,
                        'record_time': _record.record_time,
                        'user_code': $rootScope.user.UserCode
                    };
                    var param = {};
                    param.record = record;
                    param.record_detail = detail;
                    return apiService('stock/stm_take_stock_record/add_by_app', param);
                };
                //上传盘点单之后更新盘点单主表为上传
                var update_stock_record = function (record_code) {
                    return dbUpdate('stm_take_stock_record', {'is_upload': 1}, {'record_code': record_code});
                };
                //预盈亏
                var profit_and_loss_by_app = function (record, record_detail) {
                    var detail = [];
                    $.each(record_detail, function (i, n) {
                        detail.push({
                            'goods_code': n.goods_code,
                            'color_code': n.color_code,
                            'size_code': n.size_code,
                            'sku': n.sku,
                            'price': n.price,
                            'take_stock_num': n.num, //实盘数
                            'physical_count_num': n.SkuNum
                        });
                    });

                    var param = {};
                    param.record_code = record.record_code;  //盘点单据编号
                    param.record_detail = detail;
                    return apiService('stock/stm_take_stock_record/do_pre_profit_and_loss_by_app', param);
                };

                ////////////////////////////////////////////////////////ipos使用
                //获取盘点任务单列表
                var pandianTask_search_api = function (param) {
                    return apiService('stock/stm_take_stock_task_record/search_group_data', param);
                };
                //查询
                var pandianTask_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.store_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/stm_take_stock_task_record/search_group_data_by_cond', param);
                };
                //盘点任务单单据明细（根据record_code）在线查询
                var pandianTask_list_detail_search_by_code = function (param) {
                    return apiService('stock/stm_take_stock_task_record/get_detail', param);
                };
                //新增盘点单
                var add_take_stock_record = function (param) {
                    return apiService('stock/stm_take_stock_record/add_by_app', param);
                };
                //新增盘点单明细（新增盘点单放到明细里面一起新增）原来record_code
                var add_take_stock_recordDetail_api = function (record_code, record_detail) {
                    var detail = [];
                    $.each(record_detail, function (i, n) {
                        detail.push({
                            'record_code': n.record_code,
                            'goods_code': n.goods_code,
                            'color_code': n.color_code,
                            'size_code': n.size_code,
                            'sku': n.sku,
                            'price': n.price,
                            'num': n.num
                        });
                    });
                    var param = {};
                    param.record_code = record_code;
                    param.record_detail = detail;
                    return apiService('stock/stm_take_stock_record/update_by_app', param);
                };
                //未执行的任务单需要执行
                var pandianTask_do_act = function (record_code) {
                    var param = {};
                    param.record_code = record_code;
                    param.type = 'zx';
                    return apiService('stock/stm_take_stock_task_record/action', param);
                };
                //任务单预盈亏
                var pandianTask_profit_and_loss_by_app = function (record_code) {
                    var param = {};
                    param.record_code = record_code;
                    param.type = 'yyk';
                    return apiService('stock/stm_take_stock_task_record/action', param);
                };

                //任务单确认
                var pandianTask_confirm = function (record_code) {
                    var param = {};
                    param.record_code = record_code;
                    param.type = 'qr';
                    return apiService('stock/stm_take_stock_task_record/action', param);
                };

                //任务单盈亏
                var pandianTask_profit_and_loss = function (record_code) {
                    var param = {};
                    param.record_code = record_code;
                    param.type = 'yk';
                    return apiService('stock/stm_take_stock_task_record/action', param);
                };
                //重盘
                var pandianTask_cp = function (record_code) {
                    var param = {};
                    param.record_code = record_code;
                    param.type = 'cp';
                    return apiService('stock/stm_take_stock_task_record/action', param);
                };
                //根据goods_code查找商品ipos，该方法暂时不用
                var goods_search_by_code = function (keywords,brand_code) {
                    //判断是否有品牌限制
                    if(brand_code !='' && brand_code != undefined){
                    	var sql = 'select * from PosGoods where brand_code = "'+brand_code+'" and GoodsCode like "%' + keywords + '"';
                    }else{
                    	var sql = 'select * from PosGoods where GoodsCode like "%' + keywords + '"';
                    }                   
                    return dbSelect('YsPos', sql);
                };
                //根据货号获取sku、商品级盘点(根据货号精确查找,不需要根据barcode查找，因为本身传进来的就是从sku中分离的，不是输入的)
                var get_skus_by_goods_code = function (keywords) {
                    var sql = 'select p.* from PosSku p inner join PosGoods g on p.GoodsCode = g.GoodsCode where  g.GoodsCode = "' + keywords + '"';
                    return dbSelect('YsPos', sql);
                };
                //重盘之后盘点任务单下盘点单据主表更新为未上传
                var update_stock_record_unupload = function (record_code) {
                	var sql = 'select record_code from stm_take_stock_record where relation_code = "'+record_code+'"';
                	return dbSelect('YsPos',sql).then(function(response){
                		if(response.length > 0){
                			$.each(response,function(i,n){
                				dbUpdate('stm_take_stock_record', {'is_upload': 0}, {'record_code': n.record_code});
                			});
                		}
                	});
                    
                };
                return {
                    pandian_search_api: pandian_search_api,
                    pandianTask_search_api: pandianTask_search_api,
                    pandian_select: pandian_select,
                    pandianTask_select: pandianTask_select,
                    pandian_list_detail_search_by_code: pandian_list_detail_search_by_code,
                    pandianTask_list_detail_search_by_code: pandianTask_list_detail_search_by_code,
                    sku_search_by_sku: sku_search_by_sku, //商品级
                    sku_search: sku_search, //获取sku盘点数据
                    save_record_detail: save_record_detail, //保存盘点单在本地
                    take_stock_record_add_detail_api: take_stock_record_add_detail_api, //提交盘点单据到服务器
                    profit_and_loss_by_app: profit_and_loss_by_app, //预盈亏
                    update_stock_record: update_stock_record,
                    get_list_local_not_upload: get_list_local_not_upload,
                    take_stock_get_wait_upload_detail: take_stock_get_wait_upload_detail,
                    take_stock_record_by_record_code: take_stock_record_by_record_code,
                    add_take_stock_record: add_take_stock_record,
                    add_take_stock_recordDetail_api: add_take_stock_recordDetail_api,
                    pandianTask_profit_and_loss_by_app: pandianTask_profit_and_loss_by_app,
                    pandianTask_confirm: pandianTask_confirm,
                    pandianTask_profit_and_loss: pandianTask_profit_and_loss,
                    pandianTask_do_act: pandianTask_do_act,
                    pandianTask_cp: pandianTask_cp,
                    goods_search_by_code: goods_search_by_code,
                    get_skus_by_goods_code: get_skus_by_goods_code,
                    update_stock_record_unupload: update_stock_record_unupload,
                    get_save_stock_record:get_save_stock_record,
                    get_save_stock_record_detail:get_save_stock_record_detail,
                    del_save_stock_record:del_save_stock_record
                };
            }])
        .factory('adjust', ['apiService', '$rootScope', 'timeStamp2String', 'dbSelect', 'dbTrans', 'dbUpdate', 'dbDelete', 'dbInsert',
            function (apiService, $rootScope, timeStamp2String, dbSelect, dbTrans, dbUpdate, dbDelete, dbInsert) {
                //从服务器获取数据
                var adjust_search_api = function (param) {
                    return apiService('stock/stm_stock_adjust_record/search_group_data', param);
                };
                //查询
                var adjust_select = function (filter) {
                    var param = {};
                    if (typeof filter.record_time_start !== 'string' || typeof filter.record_time_end !== 'string') {
                        param.record_time_start = timeStamp2String(filter.record_time_start, 'datetime');
                        param.record_time_end = timeStamp2String(filter.record_time_end, 'datetime');
                    }
                    param.store_code = $rootScope.current_shop.ShopCode;
                    param.record_code = filter.record_code;
                    return apiService('stock/stm_stock_adjust_record/search_group_data_by_cond', param);

                };
                //调整单据明细
                var adjust_list_detail_search_by_code = function (param) {
                    return apiService('stock/stm_stock_adjust_record/get_detail', param);
                };

                return {
                    adjust_search_api: adjust_search_api,
                    adjust_select: adjust_select,
                    adjust_list_detail_search_by_code: adjust_list_detail_search_by_code
                };
            }])
        .factory('finishedReceipt', ['apiService', '$rootScope', 'timeStamp2String',
            function (apiService, $rootScope, timeStamp2String) {
                var get_sale_list = function (ret) {
                    var param = {
                        shop_code: $rootScope.current_shop.ShopCode,
                        date: ret
                    };
                    return apiService('pos/pos/get_sale_record', param);
                };
                var get_return_list = function (ret) {
                    var param = {
                        shop_code: $rootScope.current_shop.ShopCode,
                        date: ret
                    };
                    return apiService('pos/pos/get_return_record', param);
                };
                //获取更多零售销货单
                var get_more_sale_list = function (ret) {
                    var param = {
                        shop_code: $rootScope.current_shop.ShopCode,
                        date: ret
                    };
                    return apiService('pos/pos/get_last_sale_record', param);
                };
                //获取更多零售退货单
                var get_more_return_list = function (ret) {
                    var param = {
                        shop_code: $rootScope.current_shop.ShopCode,
                        date: ret
                    };
                    return apiService('pos/pos/get_last_return_record', param);
                };
                var get_sale_check_type = function (ret) {
                    var param = {
                        receipt_code: ret
                    };
                    return apiService('pos/pos/get_sale_check_type', param);
                };
                var get_return_check_type = function (ret) {
                    var param = {
                        receipt_code: ret
                    };
                    return apiService('pos/pos/get_return_check_type', param);
                };
                var get_sale_details = function (ret) {
                    var param = {
                        receipt_code: ret
                    };
                    return apiService('pos/pos/get_sale_record_detail', param);
                };
                var get_return_details = function (ret) {
                    var param = {
                        receipt_code: ret
                    };
                    return apiService('pos/pos/get_return_record_detail', param);
                };
                return {
                    get_sale_list: get_sale_list,
                    get_return_list: get_return_list,
                    get_sale_details: get_sale_details,
                    get_return_details: get_return_details,
                    get_sale_check_type: get_sale_check_type,
                    get_return_check_type: get_return_check_type,
                    get_more_sale_list: get_more_sale_list,
                    get_more_return_list: get_more_return_list
                };
            }]);
